﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAO
{
    public class ThanhPhoDAO
    {
         private FDeal4Entities db;

         public ThanhPhoDAO()
        {
            db = new FDeal4Entities();
        }

        public List<THANHPHO> LietKe()
        {
            return db.THANHPHO.ToList();
        }

        public THANHPHO LayID(string id)
        {
            var tl = db.THANHPHO.SingleOrDefault(t => t.IDTHANHPHO == id);
            return tl;
        }

        public THANHPHO TimTen(String keyWord)
        {
            var tl = db.THANHPHO.SingleOrDefault(t => t.TENTHANHPHO.Contains(keyWord));
            return tl;
        }

        public bool Them(THANHPHO tl)
        {
            try
            {
                db.THANHPHO.AddObject(tl);
                db.SaveChanges();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }

        public bool Xoa(THANHPHO tl)
        {
            try
            {
                db.THANHPHO.DeleteObject(tl);
                db.SaveChanges();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }

        public bool Sua(THANHPHO tl)
        {
            try
            {
                THANHPHO t1 = db.THANHPHO.Single(t => t.IDTHANHPHO == tl.IDTHANHPHO);
                t1.IDTHANHPHO = tl.IDTHANHPHO;
                t1.TENTHANHPHO = tl.TENTHANHPHO;

            

                db.SaveChanges();
                return true;
            }
            catch (System.Exception ex)
            {
            	return false;
            }
        }
    }
    
}
